package xyz.zyt123.hikvideo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import xyz.zyt123.hikvideo.bean.User;
import xyz.zyt123.hikvideo.common.AjaxResult;
import xyz.zyt123.hikvideo.common.PageQuery;
import xyz.zyt123.hikvideo.service.SysLogService;
import xyz.zyt123.hikvideo.service.UserService;
import xyz.zyt123.hikvideo.util.JsonUtil;
import xyz.zyt123.hikvideo.util.JwtUtil;

/**
 * @author zhuyuntao
 */
@RestController
@RequestMapping("/api/v1/user")
public class UserController {

  private static final Logger log = LoggerFactory.getLogger(UserController.class);
  @Autowired
  private UserService userService;

  @Autowired
  private SysLogService sysLogService;

  @PostMapping("/login")
  public AjaxResult<String> login(@RequestBody User user) {
    User one = userService.lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()).one();
    log.info("one:{}", JsonUtil.objectToJson(one));
    sysLogService.saveSysLog(one, "用户登录");
    if (one == null) {
      return AjaxResult.fail("密码错误!");
    }
    String jwt = JwtUtil.createJWT(JsonUtil.objectToJson(one));
    return AjaxResult.success(jwt);
  }

  @GetMapping("/getLoginUser")
  public AjaxResult<User> getLoginUser(@SessionAttribute("user") User user) {
    return AjaxResult.success(user);
  }

  @PostMapping("/register")
  public AjaxResult<Void> register(@RequestBody User user) {
    User one = userService.lambdaQuery().or()
        .eq(User::getUsername, user.getUsername())
        .or()
        .eq(User::getPhone, user.getPhone())
        .or()
        .eq(User::getEmail, user.getEmail())
        .one();
    if (one == null) {
      sysLogService.saveSysLog(user, "注册用户：" + user.getNickname());
      userService.save(user);
    } else if (one.getEmail().equals(user.getEmail())) {
      return AjaxResult.fail("邮箱不能重复");
    } else if (one.getUsername().equals(user.getUsername())) {
      return AjaxResult.fail("用户名不能重复");
    } else if (one.getPhone().equals(user.getPhone())) {
      return AjaxResult.fail("手机号不能重复");
    }
    return AjaxResult.success();
  }


  @PostMapping("/page")
  public AjaxResult<Page<User>> page(@RequestBody PageQuery pageQuery) {
    return AjaxResult.success(userService.page(new Page<>(pageQuery.getCurrent(), pageQuery.getSize())));
  }

  @GetMapping("/{id}")
  public AjaxResult<User> get(@PathVariable("id") Integer id) {
    return AjaxResult.success(userService.getById(id));
  }

  @PutMapping("/{id}")
  public AjaxResult<Void> update(@PathVariable("id") Integer id, @RequestBody User user) {
    user.setId(id);
    userService.updateById(user);
    return AjaxResult.success();
  }

  @PostMapping
  public AjaxResult<Void> add(@RequestBody User user, @SessionAttribute("user") User loginUser) {
    sysLogService.saveSysLog(loginUser, "新增用户:" + user.getNickname());
    userService.save(user);
    return AjaxResult.success();
  }

  @DeleteMapping("/{id}")
  public AjaxResult<Void> delete(@PathVariable("id") Integer id, @SessionAttribute("user") User loginUser) {
    sysLogService.saveSysLog(loginUser, "注册用户：" + loginUser.getNickname());
    userService.removeById(id);
    return AjaxResult.success();
  }
}
